﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Web;
using System.Net;
using System.Text.RegularExpressions;
using System.Globalization;

namespace RapidSpider.DownloadInitialisers
{
    public static partial class DownloadInitialiser
    {
        #region Members
        private static Regex OutlinkrComLinkRegex = new Regex(
            "(http://outlinkr.com/files/[0-9]*/[^<\"\'\\s]*)",
            RegexOptions.IgnoreCase);
        #endregion

        #region Methods
        public static ArrayList GenerateFileListFromOutlinkrLink(Uri theURI)
        {
            ArrayList theURIList = new ArrayList();

            HttpWebRequest theRequest = (HttpWebRequest)WebRequest.Create(theURI);
            theRequest.UserAgent = "Web-sniffer/1.0.28";

            using (HttpWebResponse theResponse = (HttpWebResponse)theRequest.GetResponse())
            {
                using (Stream theResponseStream = theResponse.GetResponseStream())
                {
                    using (StreamReader theStreamReader = new StreamReader(theResponseStream))
                    {
                        theURIList.AddRange(
                            DownloadInitialiser.GenerateRapidshareComFileList(
                                theStreamReader.ReadToEnd()));
                    }
                }
            }
            return DownloadInitialiser.RemoveDuplicates(theURIList);            
        }

        #region Private Helpers
        private static ArrayList GenerateOutlinkrFileList(
            string theSource)
        {
            ArrayList theURIList = new ArrayList();

            foreach (Match thisMatch in
                DownloadInitialiser.OutlinkrComLinkRegex.Matches(theSource))
            {
                string thisUri = thisMatch.Groups[1].Value;

                if (!thisUri.Contains("...") && !thisUri.Contains("…") && !thisUri.Contains(".cgi"))
                {
                    thisUri = thisUri.Replace(".html", String.Empty);

                    theURIList.AddRange(
                        DownloadInitialiser.GenerateFileListFromOutlinkrLink(
                            new Uri(thisUri)));
                }
            }

            return theURIList;
        }
        #endregion
        #endregion
    }
}
